<?php

namespace app\data\controller;

use cmf\controller\AdminBaseController;

class StaffController extends AdminBaseController
{
    function index()
    {
        return $this->fetch();
    }

    /**
     * 获取数据
     */
    function getdata()
    {
        $user = get_access_user();

        $start_time = $this->request->post('start_time', 0, 'strtotime');
        $end_time = $this->request->post('end_time', 0, 'strtotime');
        if (!$end_time) {
            if ($start_time) {
                $end_time = time() + 86399;
            }
        } else {
            $end_time += 86399;
        }

        $result = $this->getstaff($user, $start_time, $end_time);
        $this->result($result, 200);
    }

    /**
     * 获取员工数据
     * @param string $users
     * @param int $start_time
     * @param int $end_time
     * @return array
     */
    function getstaff($users = '', $start_time = 0, $end_time = 0)
    {
        $array = [
            [
                'name' => 'LicensePlate',
                'title' => '卖家'
            ],
            [
                'name' => 'Buyer',
                'title' => '买家'
            ],
            [
                'name' => 'Shoudian',
                'title' => '售电公司'
            ],
            [
                'name' => 'Medium',
                'title' => '中介'
            ],
            [
                'name' => 'Follow',
                'title' => '跟进'
            ],
            [
                'name' => 'VirtualAddress',
                'title' => '虚拟地址'
            ],
        ];
        $result = [];
        foreach ($array as $val) {
            if (!auth_check('index', "business/{$val['name']}/")) {
                continue;
            }
            $name = '\\app\\data\\model\\' . $val['name'] . 'Model';
            $model = new $name();
            $user = get_access_user();
            if (!method_exists($model, 'getStaff')) {
                continue;
            }
            $data = $model->getStaff($user, $start_time, $end_time);
            $result[$val['name']] = [
                'data' => $data,
                'title' => $val['title']
            ];
//            foreach ($data as $value) {
//                if(isset($result[$value['uid']])) {
//                    $result[$value['uid']]['count'] += $value['count'];
//                }else{
//                    $result[$value['uid']] = $value;
//                }
//                if($val['name'] == 'Follow') {
//                    $result[$value['uid']]['follow'] = $value['count'];
//                }
//            }
        }

        return array_values($result);
    }
}